/**
 * @file    ctrl_mng.c
 * @brief   控制管理
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-07-02
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-07-02 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "ctrl_mng.h"
#include "If_do.h"
#include "diagnosis.h"
#include "FreeRTOS.h"
#include "task.h"
#include "dev_afe.h"
/* macro/define --------------------------------------------------------------*/
#define SRPO_CTRL_DLY      (300)
#define BEEP_ALARM_OPEN    (1)
#define BEEP_ALARM_CLOSE   (0)

#define BEEP_OPEN_DELAY    (250)
#define BEEP_FAULT_DELAY   (1000)
#define BEEP_PRP_DELAY     (2000)
#define BEEP_ALARM_DELAY   (3000)

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/
#ifdef BEEP_USE
static uint8_t beep_open_state = 0;
static uint16_t beep_dly_cnt = 0;
static uint16_t beep_open_cnt = 0;
#endif
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
#ifdef BEEP_USE
void ctrl_beep_deal(uint16_t cycle)
{
    uint16_t delay_cycle = 0;
    if(BEEP_ALARM_OPEN == beep_open_state)
    {
        If_do_set_output(DO_BEEP_CTRL , DO_OUT_ON);
        beep_open_cnt += cycle;
        if(beep_open_cnt >= BEEP_OPEN_DELAY)
        {
            beep_open_state = 0;
            If_do_set_output(DO_BEEP_CTRL , DO_OUT_OFF);
        }
        else
        {
            If_do_set_output(DO_BEEP_CTRL , DO_OUT_ON);
        }
    }
    else
    {
        if(diag_get_prp(PRP_ID_COL) || diag_get_prp(PRP_ID_TOL) 
          || diag_get_prp(PRP_ID_FT_CMOS) || diag_get_prp(PRP_ID_FT_DMOS))
        {
            delay_cycle = BEEP_FAULT_DELAY;
        }
        else if((diag_get_prp(PRP_ID_OCC) || diag_get_prp(PRP_ID_OCD)
             ||diag_get_prp(PRP_ID_OCD2) ||diag_get_prp(PRP_ID_OTC)
             ||diag_get_prp(PRP_ID_UTC)||diag_get_prp(PRP_ID_UTD))
             ||diag_get_prp(PRP_ID_OTE)||diag_get_prp(PRP_ID_UTE)
             ||diag_get_prp(PRP_ID_OTM)||diag_get_prp(PRP_ID_VDIFF))
        {
            delay_cycle = BEEP_PRP_DELAY;
        }
        else if((diag_get_alarm(ALM_ID_OCC) || diag_get_alarm(ALM_ID_OCD)
             ||diag_get_alarm(ALM_ID_OTC)
             ||diag_get_alarm(ALM_ID_UTC)||diag_get_alarm(ALM_ID_UTD))
             ||diag_get_alarm(ALM_ID_OTE)||diag_get_alarm(ALM_ID_UTE)
             ||diag_get_alarm(ALM_ID_OTM))
        {
            delay_cycle = BEEP_ALARM_DELAY;
        }
        else
        {
            delay_cycle = 0;
        }

        if(0 != delay_cycle)
        {
            beep_dly_cnt += cycle;
            if(beep_dly_cnt > delay_cycle)
            {
                beep_dly_cnt = 0;
                beep_open_state = BEEP_ALARM_OPEN;
                If_do_set_output(DO_BEEP_CTRL , DO_OUT_ON);
            }
        }
        else 
        {
            beep_dly_cnt = 0;
            If_do_set_output(DO_BEEP_CTRL , DO_OUT_OFF);
        }
        
    }
}

#endif

void ctrl_prp_sec_spro(void)
{
	dev_afe_ctrl_mos(1 , 1);
    If_do_set_output(DO_DMOS_CTRL , DO_OUT_ON);  //补丁，弥补现在硬件缺陷
    If_do_set_output(DO_SPRO_CTRL , DO_OUT_ON);
   
    vTaskDelay(SRPO_CTRL_DLY);
	
	If_do_set_output(DO_DMOS_CTRL , DO_OUT_OFF);
    If_do_set_output(DO_SPRO_CTRL , DO_OUT_OFF);
	dev_afe_ctrl_mos(0 , 0);
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
/********** Copyright (c) 2028 JBD-Energy Storage Technology Co. LTD *********/


